﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.Infrastructure.Authentication;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.WebApp.Models;
using XSTDZ.ERP.Core.Extentions;
namespace XSTDZ.ERP.WebApp.Controllers
{
    //仓库控制器
    public class WareHoseController : Controller
    {
        private readonly IProductService wareHoseServiceImpl = ServiceLocator.Instance.GetService<IProductService>();
        //
        // GET: /WareHose/

        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [NoAuthorizated("Add")]
        public ActionResult Add()
        {
            ViewBag.StorehouseType = new RefeModel().GetDropDownListItems("DFB6639D-93AA-4113-9729-0913414ACB01");
            return View();
        }
        [HttpPost]
        [NoAuthorizated("Add")]
        public ActionResult Add(StorehouseDto storehouseDto)
        {
            if (ModelState.IsValid)
            {
                wareHoseServiceImpl.AddStorehouse(storehouseDto);
                ViewBag.PostState = 9;
            }
            ViewBag.StorehouseType = new RefeModel().GetDropDownListItems("DFB6639D-93AA-4113-9729-0913414ACB01");
            return View();
        }

        [HttpGet]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(Guid wareHourseId)
        {
            var wareHourse = wareHoseServiceImpl.GetStorehouse(wareHourseId);
            this.ViewData["StorehouseType"] = new RefeModel().GetDropDownListItems("DFB6639D-93AA-4113-9729-0913414ACB01", wareHourse.StorehouseType);
            return View(wareHourse);
        }
        [HttpPost]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(StorehouseDto storehouseDto)
        {
            if (ModelState.IsValid)
            {
                wareHoseServiceImpl.UpdateStorehouse(storehouseDto);
                ViewBag.PostState = 9;
            }
            this.ViewData["StorehouseType"] = new RefeModel().GetDropDownListItems("DFB6639D-93AA-4113-9729-0913414ACB01", storehouseDto.StorehouseType);
            return View(storehouseDto);
        }

        [HttpGet]
        [NoAuthorizated("Delete")]
        public ActionResult Delete(Guid wareHourseId)
        {
            wareHoseServiceImpl.ConvertStorehouseDeleteState(wareHourseId);
            return Content("9");
        }
        #region 返回仓库列表
        [NoAuthorizated("GetWareHoses")]
        public ActionResult GetWareHoses(string orgCode)
        {
            var wareHoses = new List<StorehouseDto>();
            if (!string.IsNullOrEmpty(orgCode) && orgCode.ToUpper()!="undefined".ToUpper())
            {
                IOrgService orgServiceImpl = ServiceLocator.Instance.GetService<IOrgService>();
                var org = orgServiceImpl.GetOrg(orgCode);
                wareHoses = wareHoseServiceImpl.GetAllStorehouse(org.Id).ToList();
            }
            else
            {
                wareHoses = wareHoseServiceImpl.GetAllStorehouse().ToList();
            }
            wareHoses = wareHoses.Where(t => t.Deleted !=1).ToList();
            if (wareHoses != null && wareHoses.Count() != 0)
            {
                var jsonResult = wareHoses;
                var storyTypes = new RefeModel().GetDropDownListItems("DFB6639D-93AA-4113-9729-0913414ACB01");
                wareHoses.Each(w =>
                {
                    w.DeletedSate = w.Deleted == 0 ? "正常" : "删除";
                    w.StorehouseTypeName = storyTypes.Where(s => s.Value == w.StorehouseType.ToString()).Select(o => o.Text).FirstOrDefault();
                });
                var target = Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult);
                target = "{\"Rows\":" + target + ",\"Total\":\"10\"}";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                return Content(target);
            }
            return Content("");
        }

      
        #endregion
    }
}
